package com.wqh.zeromq.inform;

import org.json.JSONObject;
import org.zeromq.SocketType;
import org.zeromq.ZContext;
import org.zeromq.ZMQ;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class Publisher {

    private static Map<String,Object> map = new HashMap<>();

    public static void main(String[] args) {
        startPublisher();
    }

    private static void startPublisher() {

        try(ZContext context = new ZContext()){
            //创建“发布/订阅”模式中的“发布”的socket
            ZMQ.Socket publisher = context.createSocket(SocketType.PUB);
            publisher.bind("tcp://*:5555");
            int count =1;

            while(!Thread.currentThread().isInterrupted()){
                /*
                Weather weather = new Weather();
                weather.temperature = 16.7f;
                weather.tower = "中央天气局";
                weather.windDirection = 25;
                weather.windSpeed = 10.65f;
                //发送天气信息
                 */
                pulisherRule();
                JSONObject jsonObject=new JSONObject(map);
                String topicName="app_page_"+count;
                String message="["+jsonObject.toString()+"]";
                publisher.send(topicName+"-"+message,0);
                System.out.println(">>"+topicName+"-"+message);
                count++;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void pulisherRule(){
        Random random = new Random(System.currentTimeMillis());
        map.put("hdsys_proc_netmgr_netin_0",random.nextDouble()+4.0);
        map.put("hdsys_proc_netmgr_netin_1",random.nextDouble()+4.0);
        map.put("hdsys_archive_used_0",random.nextDouble()+4.0);
        map.put("hdsys_archive_used_1",random.nextDouble()+4.0);
        map.put("hdsys_archive_left_1",random.nextDouble()+4.0);
        map.put("hdsys_proc_netmgr_netout_1",random.nextDouble()+4.0);
        map.put("hdsys_netin_1",random.nextDouble()+4.0);
        map.put("hdsys_netout_1",random.nextDouble()+4.0);
        map.put("hdsys_netout_0",random.nextDouble()+4.0);
        map.put("hdsys_proc_netmgr_netout_0",random.nextDouble()+4.0);
        map.put("hdsys_netin_0",random.nextDouble()+4.0);
        map.put("hdsys_archive_left_0",random.nextDouble()+4.0);

    }
}
